package com.ruoyi.system.controller.person;

import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.utils.uuid.UUID;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.system.domain.Econtract;
import com.ruoyi.system.domain.Emplyee;
import com.ruoyi.system.domain.IdDto;
import com.ruoyi.system.domain.KeyCard;
import com.ruoyi.system.service.IEcontractService;
import com.ruoyi.system.service.IEmplyeeService;
import com.ruoyi.system.service.IKeyCardService;
import com.ruoyi.system.utils.IdGeneratorSnowflake;
import com.ruoyi.system.vo.KeyCardVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

/**
 * 门卡配置
 *
 * @author ruoyi
 * @date 2023-10-09
 */
@RestController
@RequestMapping("/card")
public class KeyCardController extends BaseController
{
    @Autowired
    private IKeyCardService keyCardService;

    @Autowired
    private IEmplyeeService emplyeeService;

    @Autowired
    private IdGeneratorSnowflake idGeneratorSnowflake;

    /**
     * 查询门卡列表
     */
    @RequiresPermissions("system:card:list")
    @GetMapping("/list")
    public TableDataInfo list(KeyCard keyCard)
    {
        startPage();
        List<KeyCard> list = keyCardService.selectKeyCardList(keyCard);
        return getDataTable(list);
    }

    /**
     * 导出门卡列表
     */
    @RequiresPermissions("person:card:export")
    @Log(title = "门卡", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, KeyCard keyCard)
    {
        List<KeyCard> list = keyCardService.selectKeyCardList(keyCard);
        ExcelUtil<KeyCard> util = new ExcelUtil<KeyCard>(KeyCard.class);
        util.exportExcel(response, list, "门卡数据");
    }

    /**
     * 获取门卡详细信息
     */
    @RequiresPermissions("person:card:query")
    @PostMapping(value = "/getInfo")
    public AjaxResult getInfo(@RequestBody IdDto dto)
    {
        System.out.println("门卡id:"+dto.getId());
        KeyCard keyCard = keyCardService.selectKeyCardById(dto.getId());
        System.out.println("根据门卡id查询："+keyCard);
        return success(keyCard);
    }

    /**
     * 新增门卡
     */
    @RequiresPermissions("person:card:add")
    @Log(title = "门卡", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody KeyCard keyCard)
    {
        System.out.println("新增门卡："+keyCard);
        //判断输入的员工是否存在
        Emplyee emplyee = new Emplyee();
        emplyee.setName(keyCard.getName());
        List<Emplyee> emplyees = emplyeeService.selectEmplyeeList(emplyee);
        System.out.println("emplyees: "+emplyees);
        if(emplyees.isEmpty()){
            return error("该员工不存在");
        }
        //再判断是否已经配置门卡
        KeyCard keyCard1 = keyCardService.selectKeyCardByName(keyCard.getName());
        if(keyCard1 != null){
            return error("该员工已经配置门卡");
        }
        String kwStr = idGeneratorSnowflake.snowflakeId()+"";
        keyCard.setCardNumber("kw"+kwStr.substring(kwStr.length() - 6));
        keyCard.setEno(emplyees.get(0).getEno());
        keyCard.setId(idGeneratorSnowflake.snowflakeId());
        keyCard.setStartDate(new Date());
        keyCard.setCreateId("暂时无");
        System.out.println("插入keyCard："+keyCard);
        return toAjax(keyCardService.insertKeyCard(keyCard));
    }

    /**
     * 修改门卡
     */
    @RequiresPermissions("person:card:edit")
    @Log(title = "门卡", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody KeyCard keyCard)
    {
        System.out.println("修改门卡："+keyCard);
        return toAjax(keyCardService.updateKeyCard(keyCard));
    }

    /**
     * 删除门卡
     */
    @RequiresPermissions("person:card:remove")
    @Log(title = "门卡", businessType = BusinessType.DELETE)
	@DeleteMapping("/remove")
    public AjaxResult remove(@RequestBody IdDto dto)
    {
        return toAjax(keyCardService.deleteKeyCardById(dto.getId()));
    }
}
